# coding=utf-8

"""
利用武汉市土地利用类型的栅格文件，按照利用类型分别提取出来作为新的栅格文件

"""

import arcpy
import os
from arcpy.sa import *

inRaster = u"E:\\毕设数据\\wuhan_shp\\武汉市土地利用类型\\武汉市Land2019 _Raster.tif"
out_root = u"E:\\毕设数据\\wuhan_shp\\武汉市土地利用类型\\using_type"
# inSQLClause = "land2019='51'"

# Check out the ArcGIS Spatial Analyst extension license
# arcpy.CheckOutExtension("Spatial")

# Execute ExtractByAttributes
# attExtract = ExtractByAttributes(inRaster, inSQLClause)

# Save the output
# attExtract.save(u"C:\\Users\\91481\\Desktop\\test\\using\\test.tif")

using_map1 = {
    '1': u'耕地',

    '2': u'林地',

    '3': u'草地',

    '4': u'水域',

    '5': u'城乡_工矿_居民用地',

    '6': u'未利用土地'

}

using_map2 = {
    '11': u'水田',
    '12': u'旱地',
    '21': u'有林地',
    '22': u'灌木林地',
    '23': u'疏林地',
    '24': u'其他林地',
    '31': u'高覆盖度草地',
    '32': u'中覆盖度草地',
    '33': u'地覆盖度草地',
    '41': u'河渠',
    '42': u'湖泊',
    '43': u'水库_坑塘',
    '44': u'冰川永久积雪',
    '45': u'海涂',
    '46': u'滩地',
    '51': u'城镇',
    '52': u'农村居民点',
    '53': u'工交建设用地',
    '61': u'沙地',
    '62': u'戈壁',
    '63': u'盐碱地',
    '64': u'沼泽地',
    '65': u'裸土地',
    '66': u'裸岩石砾地',
    '67': u'其他未利用地'

}


def value_sort():
    land_2019_list_raw = ['11', '12', '21', '22', '23', '24', '31', '32', '33',
                          '41', '42', '43', '46', '51', '52', '53', '64', '65', '66']
    s1 = []
    s2 = []
    s3 = []
    s4 = []
    s5 = []
    s6 = []
    for r in land_2019_list_raw:
        if r[0] == '1':
            s1.append(r)
        elif r[0] == '2':
            s2.append(r)
        elif r[0] == '3':
            s3.append(r)
        elif r[0] == '4':
            s4.append(r)
        elif r[0] == '5':
            s5.append(r)
        elif r[0] == '6':
            s6.append(r)
    return s1, s2, s3, s4, s5, s6


def extract():
    arcpy.CheckOutExtension("Spatial")
    land_2019_list = value_sort()

    for i in land_2019_list:
        sql = """"""
        for j in i:
            sql += """"land2019"='%s' OR """  % j
        inSQLClause = sql[:-3]
        # print sql
        # inSQLClause = "land2019=%s" % i
        # print inSQLClause
        file_name = i[0][0] + "_" + using_map1[i[0][0]] + ".tif"
        save_name = os.path.join(out_root, file_name)
        print save_name
        print inSQLClause
        #
        attExtract = ExtractByAttributes(inRaster, inSQLClause)
        attExtract.save(save_name)


def exec_sql():
    sql = '''"land2019" = '41' OR "land2019" = '42' OR "land2019" = '43' OR "land2019" = '46' '''
    print sql
    print os.path.join(out_root, "test.tif")
    attExtract = ExtractByAttributes(inRaster, sql)
    save_name = os.path.join(out_root, "test.tif")
    attExtract.save(save_name)


if __name__ == '__main__':
    # exec_sql()
    extract()